package com.echat.serviceapigateway.security.model.authorization;

import com.echat.serviceapigateway.security.model.BaseModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Id;
import java.time.LocalDateTime;

/**
 * Created by chenmohan on 2018/08/31
 *
 * @description
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Resource extends BaseModel {

    @Id
    private Long id;

    private Long serviceId;

    @Column(name = "resource_uri")
    private String uri;

    private String memo;

    @Column(name = "create_datetime")
    protected LocalDateTime createTime;

    @Column(name = "modify_datetime")
    protected LocalDateTime modifyTime;

    /**
     * 得到服务前缀uri，比如"/send/aa/bb/cc",那么方法返回 "/send"
     *
     * @return
     */
    public String serviceUri() {
        if (this.uri == null || this.uri.trim().length() == 0) {
            return null;
        }
        int endIndex = this.uri.trim().indexOf('/', 2);
        if (endIndex == -1) {
            return this.uri;
        } else {
            return this.uri.trim().substring(0, endIndex);
        }
    }

}
